package com.tanhua.locationmanager.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.vo.LocationManagerVo;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LocationService {

    @Reference
    private UserLocationApi userLocationApi;

    @Reference
    private UserInfoApi userInfoApi;

    /*
    根据Id查询地址位置，经纬度
    by Dzh
     */
    public ResponseEntity queryById(Long id) {
        Map<String,Double> map = userLocationApi.queryById(id);
        return ResponseEntity.ok(map);
    }

    /*
    查询所有用户的信息，包括当前经纬度
    by Huo
     */
    public ResponseEntity<?> list(Integer page, Integer pageSize) {
        PageResult result = userLocationApi.queryByPage(page,pageSize);
        List<LocationManagerVo> locations = (List<LocationManagerVo>) result.getItems();
        for (LocationManagerVo location : locations) {
            UserInfo userInfo = userInfoApi.findById(location.getId());
            BeanUtils.copyProperties(userInfo,location,"userStatus");
            location.setState(userInfo.getUserStatus());
            location.setGender(location.getGender().equals("woman")?"女":"男");
            location.setState(location.getState().equals("1")?"正常":"冻结");
        }
        return ResponseEntity.ok(result);
    }
}
